import http, {ApiResponse, ApiResponsePage} from "../../../../lang/http";

export interface UserDept {
    // ID
    id: string;
    // 层级路径
    path: string;
    // 父级ID
    pid: string;
    // 层级
    level: string;
    // 角色ID
    roleId: string;
    // 机构ID
    orgId: string;
    // 部门名称
    deptName: string;
    // 部门编码
    deptCode: string;
    // 图标
    deptIcon: string;
    // 描述
    description: string;
    // 是否可编辑
    editable: boolean;
    // 是否禁用
    disabled: boolean;
    // 创建日期
    gmtCreate: string;
    // 修改日期
    gmtModified: string;
}

const formModel = (): UserDept => {
    return {
        //ID
        id: ''
        //层级路径
        , path: ''
        //父级ID
        , pid: ''
        //层级
        , level: ''
        //角色ID
        , roleId: ''
        //机构ID
        , orgId: ''
        //部门名称
        , deptName: ''
        //部门编码
        , deptCode: ''
        //图标
        , deptIcon: ''
        //描述
        , description: ''
        //是否可编辑
        , editable: true
        //是否禁用
        , disabled: true
        //创建日期
        , gmtCreate: ''
        //修改日期
        , gmtModified: ''
    }
}

const formRules = {
    id: [
        {required: true, message: "[ID]字段不允许为空"},
        {min: 0, max: 32, message: "[ID]最多32字符"}
    ],
    path: [
        {min: 0, max: 32, message: "[层级路径]最多32字符"}
    ],
    pid: [
        {min: 0, max: 32, message: "[父级ID]最多32字符"}
    ],
    roleId: [
        {min: 0, max: 32, message: "[角色ID]最多32字符"}
    ],
    orgId: [
        {min: 0, max: 32, message: "[机构ID]最多32字符"}
    ],
    deptName: [
        {min: 0, max: 32, message: "[部门名称]最多64字符"}
    ],
    deptCode: [
        {min: 0, max: 32, message: "[部门编码]最多32字符"}
    ],
    icon: [
        {min: 0, max: 32, message: "[图标]最多32字符"}
    ],
    description: [
        {min: 0, max: 32, message: "[描述]最多128字符"}
    ],
}

/**
 * 组织机构的映射函数
 */
const deptTreeMapper: TreeMapper = (ele: UserDept, ret: TreeNode): void => {
    console.log(ele);
    ret.data = ele;

    ret.id = ele.id;
    ret.pid = ele.pid;

    ret.icon = ele.deptIcon;
    ret.label = ele.deptName;
    ret.checked = false;
}

/**
 * 数据新增
 *
 * @param pid 父级 ID
 * @returns 请求Promise
 */
const create = (pid:string): Promise<ApiResponse> => {
    return http.ajax({
        url: '/sys/dept/info',
        method: 'POST',
        params: {pid}
    });
}

/**
 * 根据 ID 删除
 *
 * @param id 数据 ID
 * @returns 请求Promise
 */
const remove = (id: string | number): Promise<ApiResponse> => {
    return http.ajax({
        method: "DELETE",
        url: 'sys/dept/info',
        params: {id}
    });
}

/**
 * 数据修改
 *
 * @param params 对象实体
 * @returns 请求Promise
 */
const modify = (params: UserDept): Promise<ApiResponse> => {
    return http.ajax({
        method: "PUT",
        url: 'sys/dept/info',
        params: params
    });
}

/**
 * 数据列表查询
 *
 * @param params 查询参数
 * @returns 分页数据Promise
 */
const list = (params: object): Promise<ApiResponsePage> => {
    return http.ajax({
        url: '/sys/dept/list',
        method: 'GET',
        params
    });
}

export default {create, remove, modify, list, formModel, formRules, deptTreeMapper}